<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <button>修改history</button>
  <button class="back">返回上一级</button>
  <button class="forward">前进</button>
  <script>
    // 前端路由核心:修改了URL 但是页面不刷新
    // 1.修改hash值
    // 2.修改history
    console.log(history.length)
    console.log(history.state)
    var btnEl = document.querySelector("button")
    var backBtnEl = document.querySelector(".back")
    var forwardBtnEl = document.querySelector(".forward")
    btnEl.onclick = () => {
      history.pushState({ name: "why", age: 18 }, "", "/why")
      console.log(history.length)
      console.log(history.state)
    }
    backBtnEl.onclick = () => {
      history.back()
    }
    forwardBtnEl.onclick = () => {
      history.forward() // history.go(1)
    }
  </script>
</body>

</html>